var _global;
var _gl = require('opengl');
var _framerate = require('tools/framerate.js');

var _MessagUtil = require('utils/message.js');
var _History = require('config/history.js');
var _Statistics = require('config/statistics.js');
var _Prefer = require('config/prefer.js');

var _Messages = require('config/messages.js');
var _UserInfo = require('config/userinfo.js');
var _BackListener = require('global/backlistener.js');

var gameSetup = true;

function Game() {
    _History.load();
    _Statistics.load();
    _Prefer.load();
}
// ---------------- protocal handler
Game.prototype.onRequest = function (id, data) {
//    if (data) {
//        console.log('onRequest', id, JSON.stringify(data));
//    } else {
//        console.log('onRequest', id);
//    }
    switch (id) {
        case _Messages.ID_UI_BACK_CLICK:
            if (_global) {
                _BackListener.onBackPressed(_global);
            }
            break;
        case _Messages.ID_NAMECHANGED:
            _global.scheduleTask(function () {
                _UserInfo.setName(data.name);
            });
            break;
        case _Messages.ID_NAMECANCELED:
            _global.scheduleTask(function () {
                _UserInfo.cancelName();
            });
            break;
    }
};
Game.prototype.processRequest = function (str) {
    var messages = _MessagUtil.parse(str);
    if (messages instanceof Array) {
        for (var i = 0; i < messages.length; i++) {
            var msg = messages[i];
            this.onRequest(msg[_Messages.MESSAGE_ID], msg[_Messages.MESSAGE_DATA]);
        }
    } else {
        this.onRequest(messages[_Messages.MESSAGE_ID], messages[_Messages.MESSAGE_DATA]);
    }
};

// ---------------- life time method
Game.prototype.onSurfaceCreated = function (width, height) {
    //console.log('Game.prototype.onSurfaceCreated', width, height, gameSetup);

    // setup gl status whatever
    _gl.clearColor(1, 1, 1, 1);
    _gl.enable(_gl.BLEND);
    _gl.blendFunc(_gl.ONE, _gl.ONE_MINUS_SRC_ALPHA);
    _gl.disable(_gl.DEPTH_TEST);
    _gl.disable(_gl.STENCIL_TEST);
    _gl.disable(_gl.SCISSOR_TEST);

    if (!gameSetup) {
        try {
            _global.recoverContext();
            _global.onScreenSizeChange(width, height);// init sreen size
            _global.reclock();
        } catch (e) {
            console.log(e);
        }
    } else {
        _global = require('global/global.js');
        _global.onScreenSizeChange(width, height);// init sreen size

        var initscene;
        if (_Prefer.isToturialfinished()) {
            initscene = require('scenes/cover.js').newInstance();
        } else {
            var gameLocal = require('scenes/game/constant.js').getLocal();
            gameLocal.add('istoturial', true);
            initscene = require('scenes/game.js').newInstance();
            _BackListener.bindScene(initscene);
            _BackListener.setActive(true);
        }

        _global.reclock();
        _global.registerScene(initscene);
        gameSetup = false;
    }
};
Game.prototype.onSurfaceChanged = function (width, height) {
    _global.onScreenSizeChange(width, height);
};
Game.prototype.onDrawFrame = function () {
    _global.runSchedule();
    //_framerate.update();
};
Game.prototype.pause = function () {
    //console.log('Game.prototype.pause=>');
};
Game.prototype.resume = function () {
    //console.log('Game.prototype.resume=>');
};
Game.prototype.toString = function () {
    return '[GAME]';
};

var game = new Game();
module.exports = game;
